<template>
  <div>
    <Breadcrumb></Breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 用户搜索添加 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-input placeholder="请输入内容" class="input-with-select" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="8">
          <el-button type="primary" @click="getAddUser()">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table表格 -->
      <template>
        <el-table :data="cateData.goods" border style="width: 100%">
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column prop="goods_name" label="商品名称" width="500"></el-table-column>
          <el-table-column prop="goods_price" label="商品价格(元)"></el-table-column>
          <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
          <el-table-column prop="upd_time" label="创建时间"></el-table-column>
          <el-table-column label="操作"></el-table-column>
        </el-table>
      </template>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodslist.pagenum"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="goodslist.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.cateData.total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getGoodsData } from "@/api/Goods/goods.js";
import Breadcrumb from "@/components/Breadcrumb";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: { Breadcrumb },
  props: {},
  data() {
    //这里存放数据
    return {
      goodslist: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      cateData: [] //商品列表数据
    };
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    async getgoods() {
      var { data: res } = await getGoodsData(this.goodslist);
      console.log(res);
      if (res.meta.msg != "获取成功")
        return this.$message.error("商品列表获取失败");
      this.$message.success("获取商品列表成功");
      this.cateData = res.data;
      console.log(this.cateData);
    },
    //分页器
    getnowpage(nowpage) {
      this.goodslist.pagenum = nowpage;
      this.getgoods();
    },
    handleSizeChange(val) {
      this.goodslist.pagesize = val;
      this.getgoods();
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.goodslist.pagenum = val;
      this.getgoods();
      console.log(`当前页: ${val}`);
    },
    // 添加商品
    getAddUser() {
      this.$router.push("/add");
      var bread = { oneName: "商品管理", twoName: "添加商品" };
      sessionStorage.setItem("path", JSON.stringify(bread));
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getgoods();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
.el-card {
  margin-top: 20px;
  background: rgba(0, 0, 0, 0.5);
}
.el-table {
  margin-bottom: 20px;
  margin-top: 20px;
  background: rgba(0, 0, 0, 0.5);
}
</style>